import os
import requests
import json
import pandas as pd
from MyConfig import MyConfig
from NeuroConsolidator import NeuroConsolidator
from OraWorker import OraWorker

cur_path = os.path.dirname(os.path.realpath(__file__))
conf =  MyConfig(f"{cur_path}\\config.json")
api_url_method= conf.get("NeuroApiUrl",None)
neuro = NeuroConsolidator(api_url_method)
ora = OraWorker("manage_dw","ufyjwhb","172.17.80.116/scpora",".\\ora")

def handler(rec:pd.Series)->pd.Series:
    try:        
        result = neuro.send_single_request(rec,"Вид материала в SAP ERP","Полное наименование материала",n_predictions=2)
        product= result[0][0]
        probability = result[0][1]  
        product2= result[1][0]
        probability2 = result[1][1]        
        print(f"{rec['Полное наименование материала']} -> {product} : {probability}%")
        # print(result)
        rec["Продукт УП 1"]=product
        rec["Вероятность 1"]=probability
        rec["Продукт УП 2"]=product2
        rec["Вероятность 2"]=probability2
        check = ora.product_check(product,str(rec["ID в SAP ERP VMZ"]))
        rec["Есть в УП"] = check[1]
        rec["Уже укрупнено в"] = check[0]
        rec["Совпадение"] = check[0]==product               
        return rec
    except Exception as exp:
        rec["Продукт УП"]=f"error: {exp}"
        rec["Вероятность"]=100
        return  rec


if __name__=="__main__":
    try:
        input_file = f"{cur_path}\\output\\input_data.xlsx"
        input_file = "D:\\work\\Укрупнение\\current\\debug_data\\neuro_dump.xlsx"
        print(f"work with file: {input_file}")
        df = pd.read_excel(input_file)
        df = df[["Вид материала в SAP ERP","Полное наименование материала","ID в SAP ERP VMZ"]]
        df = df.apply(handler,axis=1)
        df.to_excel(f"{cur_path}\\output\\neuro_data.xlsx",index=False)
        ora.close()
        
    except Exception as exp:
        print(f"general exception: {exp}")